import SkuList from './SkuList.vue'
import SkuListPermission from './SkuListPermission.vue' //sku 限购权限
import SkuListMemberShip from './SkuListMemberShip.vue' //sku 会员权限
import SkuListBkerage from './SkuListBkerage.vue' //sku分销设置
import SkuListPaidMemberShip from './SkuListPaidMemberShip.vue' //sku付费会员权限
import CouponList from './CouponList.vue' //优惠券列表
import SpuAndSkuList from '@/views/mall/promotion/components/SpuAndSkuList.vue'
import SpuSelect from '@/views/mall/promotion/components/SpuSelect.vue'
import ProductSpuTypeSelect from './ProductSpuTypeSelect.vue' //商品类型选择

import { Spu } from '@/api/mall/product/spu'

interface PropertyAndValues {
  id: number
  name: string
  values?: PropertyAndValues[]
}

interface RuleConfig {
  // 需要校验的字段
  // 例：name: 'name' 则表示校验 sku.name 的值
  // 例：name: 'productConfig.stock' 则表示校验 sku.productConfig.name 的值,此处 productConfig 表示我在 Sku 上扩展的属性
  name: string
  // 校验规格为一个毁掉函数，其中 arg 为需要校验的字段的值。
  // 例：需要校验价格必须大于0.01
  // {
  //  name:'price',
  //  rule:(arg: number) => arg > 0.01
  // }
  rule: (arg: any) => boolean
  // 校验不通过时的消息提示
  message: string
}

type SpuProperty<T> = {
  spuId: number
  spuDetail: T
  propertyList: PropertyAndValues[]
}

/**
 * 获得商品的规格列表 - 商品相关的公共函数
 *
 * @param spu
 * @return PropertyAndValues 规格列表
 */
const getPropertyList = (spu: Spu): PropertyAndValues[] => {
  //  直接拿返回的 skus 属性逆向生成出 propertyList
  const properties: PropertyAndValues[] = []
  // 只有是多规格才处理

  if (spu.specType) {
    spu.skus?.forEach((sku) => {
      sku.properties?.forEach(({ propertyId, propertyName, valueId, valueName }) => {
        // 添加属性
        if (!properties?.some((item) => item.id === propertyId)) {
          properties.push({ id: propertyId!, name: propertyName!, values: [] })
        }
        // 添加属性值
        const index = properties?.findIndex((item) => item.id === propertyId)
        if (!properties[index].values?.some((value) => value.id === valueId)) {
          properties[index].values?.push({ id: valueId!, name: valueName! })
        }
      })
    })
  }
  return properties
}
const fromDataRule = {
  /**浏览权限 */
  browseAuthority: [0], //浏览权限 0 不限制 1 指定会员等级可见 2 指定付费会员等级可见 4指定标签组可见
  browseMember: undefined, //浏览权限 会员
  browsePaidMember: undefined, //浏览权限 付费会员
  browseTags: undefined, //浏览权限 标签组
  /** 购买权限 */
  buyAuthority: [0], //购买权限
  buyMember: undefined, //购买权限 会员
  buyPaidMember: undefined, //购买权限 付费会员
  buyTags: undefined, //购买权限 标签组
  /** 商品限购 */
  quotaAuthority: 0, //商品限购
  singleQuotaMini: undefined, //商品限购 单次最少
  singleQuotaMax: undefined, //商品限购 单次最多
  permanentQuota: undefined, //商品限购 永久限购
  /** 营销设置 */
  memberPointsGain: 0, //会员积分获取
  memberPointsGainEntry: {}, //会员积分获取单独设置
  paidmemberPointsGain: 0, //付费会员积分获取
  paidmemberPointsGainEntry: {}, //付费会员积分获取单独设置,
  memberWalletRule: 0, //会员余额抵扣
  memberWalletRuleEntry: {}, //会员抵扣单独设置,
  paidmemberWalletRule: 0, //付费会员余额抵扣
  paidmemberWalletRuleEntry: {}, //付费会员余额抵扣单独设置
  /** 会员折扣设置 */
  memberDiscount: 0, //会员折扣
  memberDiscountEntry: {}, //会员折扣单独设置
  /** 付费会员折扣设置 */
  paidmemberDiscount: 0, //付费会员折扣设置
  paidmemberDiscountEntry: {} //付费会员折扣单独设置
} //商品规则默认 用于规格变化时 重置规则
export {
  SkuList,
  SpuAndSkuList,
  SpuSelect,
  SkuListPermission,
  SkuListMemberShip,
  SkuListPaidMemberShip,
  SkuListBkerage,
  CouponList,
  ProductSpuTypeSelect,
  PropertyAndValues,
  RuleConfig,
  SpuProperty,
  getPropertyList,
  fromDataRule
}
